﻿using Microsoft.AspNetCore.Mvc;
using Zhp.Api.Attributes;
using Zhp.IService.Message.Email;
using Zhp.Model.Common;
using Zhp.Model.Message.Email;

namespace Zhp.Api.Controllers.Message.Email
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    [ApiGroup(GroupName = ApiGroupEnum.Message)]
    [SeedMenu(MenuGroup = ApiGroupEnum.Message, Name = "邮箱账户管理", Order = 1, RouterName = "emailaccount", WebFilePath = "message/email/emailAccount/index", Icon = "email-account")]
    public class EmailAccountController : ControllerBase
    {
        protected readonly IEmailAccountService _emailAccountService;

        public EmailAccountController(IEmailAccountService emailAccountService)
        {
            _emailAccountService = emailAccountService;
        }

        [HttpPost]
        [SeedList("邮箱账户列表", "邮箱账户管理", ApiGroupEnum.Message)]
        public async Task<PageRet<EmailAccountModel>> Query(QueryEmailAccountParam p)
        {
            var list = await _emailAccountService.Query(p);
            return new PageRet<EmailAccountModel>
            {
                DataList = list,
                TotalCount = p.TotalCount,
            };
        }

        [HttpPost]
        [SeedButton("新增", "邮箱账户列表", "邮箱账户管理", ApiGroupEnum.Message)]
        public async Task<bool> Create(EditEmailAccountParam p)
        {
            return await _emailAccountService.Create(p);
        }

        [HttpPut]
        [SeedButton("修改", "邮箱账户列表", "邮箱账户管理", ApiGroupEnum.Message)]
        public async Task<bool> Update(EditEmailAccountParam p)
        {
            return await _emailAccountService.Update(p);
        }

        [HttpDelete("{id}")]
        [SeedButton("删除", "邮箱账户列表", "邮箱账户管理", ApiGroupEnum.Message)]
        public async Task<bool> Delete(long id)
        {
            return await _emailAccountService.Delete(id);
        }

        [HttpGet]
        public async Task<List<EmailAccountSelect>> QueryAccountSelect()
        {
            return await _emailAccountService.QueryAccountSelect();
        }

        [HttpGet]
        [SeedButton("导出", "邮箱账户列表", "邮箱账户管理", ApiGroupEnum.Message)]
        public async Task<IActionResult> ExportExcel()
        {
            var (stream, contentType, fileName) = await _emailAccountService.ExportExcel();

            return File(stream, contentType, fileName);
        }

        [HttpPost]
        [SeedButton("导入", "邮箱账户列表", "邮箱账户管理", ApiGroupEnum.Message)]
        public async Task<bool> ImportExcel(IFormFile file)
        {
            return await _emailAccountService.ImportExcel(file.OpenReadStream());
        }
    }
}
